package com.gitee.melin.bee.core.hibernate5.type;

import com.google.common.collect.Sets;
import java.util.Set;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import org.apache.commons.lang3.StringUtils;

@Converter
public class StringSetConverter implements AttributeConverter<Set<String>, String> {

    @Override
    public String convertToDatabaseColumn(Set<String> list) {
        if (list == null) {
            return null;
        } else {
            return String.join(",", list);
        }
    }

    @Override
    public Set<String> convertToEntityAttribute(String joined) {
        if (StringUtils.isNotBlank(joined)) {
            return Sets.newHashSet(joined.split(","));
        } else {
            return null;
        }
    }
}
